package com.itqf.controller;

import com.alibaba.fastjson.JSONObject;
import com.itqf.service.*;
import com.itqf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: store_parent
 *
 * @author: 赵伟风
 * time: 2021/6/9 11:02
 * description:展示面板的controller
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/dashboard")
public class DashboardController {


    private Logger logger = LoggerFactory.getLogger(DashboardController.class);

    @Autowired
    private DtsUserService userService;
    @Autowired
    private DtsOrderService orderService;
    @Autowired
    private DtsGoodsService goodsService;
    @Autowired
    private DtsGoodsProductService goodsProductService;

    @Autowired
    private DashboardService dashboardService;

    @RequiresAuthentication
    @GetMapping("")
    public Object showData(){

        logger.info("[面板数据-数量统计] 接口开始调用！");

        int userNumber = userService.findUserNumber();
        int orderNumber = orderService.findOrderNumber();
        int goodsNumber = goodsService.findGoodsNumber();
        int goodsProductNumber = goodsProductService.findGoodsProductNumber();

        Map<String,Integer> data = new HashMap<>();
        data.put("goodsTotal",goodsNumber);
        data.put("userTotal", userNumber);
        data.put("productTotal", goodsProductNumber);
        data.put("orderTotal", orderNumber);


        Object ok = ResponseUtil.ok(data);
        logger.info("[面板数据-数量统计] 接口结束调用！获取的数据为：{}！", JSONObject.toJSON(ok).toString());

        return ok;
    }


    @RequiresAuthentication
    @GetMapping("chart")
    public Object chart(@RequestParam(defaultValue = "30") String day){

        logger.info("[面板数据-图标数据]开始调用！查询的天数为：{}", day);
        int days = 0;
        try {
            days =  Integer.parseInt(day);
            if (days<=0) {
                logger.error("[面板数据-图标数据]传递参数异常，参数为：{},为负数了！",days);
                return  ResponseUtil.badArgument();
            }
        }catch (NumberFormatException e)
        {
            logger.error("[面板数据-图标数据]传递参数异常，参数为：{},不是整数！",days);
            //传入的不是数字或者不是整数
            return  ResponseUtil.badArgument();
        }

        Map<String,Object> data = dashboardService.ShowChart(days);

        Object ok = ResponseUtil.ok(data);

        logger.info("[面板数据-图标数据]结束调用！返回数据为：{}", JSONObject.toJSON(ok).toString());
        return  ok;

    }


}



